package com.aier.admin_new.service.impl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.views.xslt.ArrayAdapter;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Component;

import com.aier.admin_new.service.IMessageNService;
import com.aier.common.dao.ICommonDAO;
import com.aier.user.entity.Letter;
import com.aier.user.entity.mapper.LetterDetailRowMapper;
import com.aier.user.entity.mapper.LetterRowMapper;

@Component("messageNService")
public class MessageNServiceImpl implements IMessageNService {
    @Resource(name = "commonDao")
    private ICommonDAO commonDao;
    
	@Override
	public int insertMessage(Long suid, Long ruid, Short type, String content,
			Short state, String img) {
		 Letter letter = (Letter) commonDao.listByObject("select * from letter where ((suid = ? and ruid=?) or (suid = ? and ruid = ?)) and type=1",
	                new Object[]{suid,ruid,ruid,suid},new LetterRowMapper());
	        if (null != letter) {
	            // 两人之间已存在对话
	            commonDao.update("update letter set suid=?,ruid=?,content=?,state=0,sdel=0,rdel=0,img=?,cdate=? where lid=?",
	                    new Object[]{suid,ruid,content,img,new Timestamp(new Date().getTime()),letter.getLid()});
	            commonDao.update("insert into letter_detail(suid,ruid,content,state,sdel,rdel,img,cdate,lid) values (?,?,?,?,?,?,?,?,?)",
	                    new Object[]{suid,ruid,content,state,0,0,img,new Timestamp(new Date().getTime()),letter.getLid()});
	            return Integer.parseInt(letter.getLid()+"");
	        } else {
	            // 两人之间不存在对话
	            Letter let = new Letter();
	            let.setCdate(new Timestamp(new Date().getTime()));
	            let.setContent(content);
	            let.setImg(img);
	            let.setRdel((short)0);
	            let.setRuid(ruid);
	            let.setSdel((short)0);
	            let.setState((short)0);
	            let.setSuid(suid);
	            let.setType((short)1);
	            KeyHolder kh = commonDao.insert(LetterRowMapper.getPreparedStatementCreator(let),new GeneratedKeyHolder());
	            commonDao.update("insert into letter_detail(suid,ruid,content,state,sdel,rdel,img,cdate,lid) values (?,?,?,?,?,?,?,?,?)",
	                    new Object[]{suid,ruid,content,state,0,0,img,new Timestamp(new Date().getTime()),kh.getKey().longValue()});
	            return kh.getKey().intValue();
	        }
	}

	@Override
	public List<Letter>  searchFollowMessage(Long uid) {
		
		List<Letter> list =(List<Letter>) commonDao.list("select l.*, u.nickname, u.headimg from letter l left join user u on u.uid = l.suid " +
        		"where l.ruid = ? and l.type = 1 union select l.*, u.nickname, u.headimg from letter l left join user u " +
        		"on u.uid = l.ruid where (l.suid = ? ) and l.type = 1 order by cdate desc ",new Object[]{uid,uid},new LetterRowMapper());
		
		List<Letter> lisd=(List<Letter>) commonDao.list("select l.*, u.nickname, u.headimg from letter l left join user u on "
				+ "u.uid = l.suid where l.ruid = ? and l.type = 5 union select l.*, u.nickname, u.headimg "
				+ "from letter l left join user u  on u.uid = l.ruid where (l.suid = ? ) and l.type = 5 "
				+ "order by cdate desc ",new Object[]{uid,uid},new LetterRowMapper());
		
		List<Letter> os = new ArrayList<Letter>();

		if(lisd.size()>0){
			os.add(0, lisd.get(0));
		}
		
       if(list.size()>0){
    	   for(Letter lr :list){
    		   os.add(lr);
    	   }
       }
       
		return os;
	}

	@Override
	public Map<String, Object> searchMessageDetail(int page, int pagesize,Long lid) {
		  return commonDao.queryPage("select ld.*,u.nickname,u.headimg from letter_detail ld left join letter l " +
	        		"on l.lid = ld.lid left join `user` u on u.uid = ld.suid where ld.lid = ? and (( ld.suid = l.suid and ld.ruid = l.ruid " +
	        		"and ld.sdel = 0 ) or ( ld.ruid = l.suid and ld.suid = l.ruid and ld.rdel = 0 )) order by ld.cdate desc ",
	        		page,pagesize,new Object[]{lid},new LetterDetailRowMapper());
	}

	@Override
	public int deleteMessageByLdid(Long uid, String ldidStr) {
		return 0;
	}

	@Override
	public int deleteMessage(Long uid) {
		return 0;
	}

	@Override
	public int searchFlter(Long uid) {
		int s=commonDao.countByQuali("select count(*)  from  letter where suid=? and ruid = 1801 ", new Object[]{uid});
		return s;
	}

	@Override
	public List searchSystemMessage(Long uid) {
		List<Letter> lisd=(List<Letter>) commonDao.list("select l.*, u.nickname, u.headimg from letter l , user u"
				+ "  where  u.uid = l.suid   and  l.ruid = ? and l.type = 5 order by cdate desc  ",new Object[]{uid},new LetterRowMapper());
		return lisd;
	}
}
